How to set the logic for binary search program